package com.jinbo.sxzq;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

public class LevelSelectAdapter extends ArrayAdapter<LevelSelectItem> {
    private int resourceId;

    public LevelSelectAdapter(@NonNull Context context, int resource, @NonNull List<LevelSelectItem> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LevelSelectItem levelSelectItem = getItem(position); //获取当前项的levelSelectItem实例
        View view;
        ViewHolder viewHolder;
        if (convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.level = (TextView) view.findViewById(R.id.adapter_level);
            viewHolder.levelName =(TextView) view.findViewById(R.id.adapter_levelname);
            view.setTag(viewHolder);// 将ViewHolder存储在View中。
        }else
        {
            view = convertView;
            viewHolder=(ViewHolder)view.getTag(); //重新获取ViewHolder

        }
        viewHolder.level.setText("第" + levelSelectItem.getLevel() + "关：");
        viewHolder.levelName.setText(levelSelectItem.getLevelName());
        return view;
    }

    class ViewHolder{
        TextView levelName;
        TextView level;
    }
}
